package tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class PhoneUtils {

    public static String desensitizedMobilePhone(String mobilePhone) {
        if (isNotBlank(mobilePhone)) {
            return mobilePhone.replaceAll("(\\d{3})\\d*(\\d{4})", "$1******$2");
        }
        return mobilePhone;
    }

    /**
     * 验证手机号
     *
     * @param mobilePhone
     * @return
     */
    public static boolean valiMobilePhone(String mobilePhone) {
        if (mobilePhone == null) return false;
        return Pattern.compile("^(13|14|15|16|17|18|19)\\d{9}$").matcher(mobilePhone).find();
    }

    /**
     * 验证固话号码
     *
     * @param telephone
     * @return
     */
    public static boolean valiTelephone(String telephone) {
        if (telephone == null) return false;
        String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(telephone);
        return matcher.matches();
    }

}
